package com.springboot.autoconfigure.oss.util;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * @Description:
 * @Author: zehua
 * @Created: 2023/12/21 16:44
 */
public class DirectoryUtil {

    /**
     *获取目录下的层级目录
     */
    public static TreeMap<String,Integer> listDirectory(String path, int level, TreeMap<String,Integer> result) {
        File directory = new File(path);
        if (!directory.isDirectory()) {
            System.out.println("提供的路径不是一个有效的目录：" + path);
            return null;
        }
        File[] files = directory.listFiles();
        if (files == null) {
            return null;
        }
        // 按照文件夹优先，然后按名称排序
        Arrays.sort(files, Comparator.comparing(File::isFile).thenComparing(File::getName));
        for (File file : files) {
            // 如果是目录，则递归调用
            if (file.isDirectory()) {
                result.put(file.getAbsolutePath(),level);
                listDirectory(file.getAbsolutePath(), level + 1,result);
            }
        }
        return result;
    }
}
